<template>
  <div class="admin-layout">
    <!-- 侧边栏 -->
    <el-aside width="200px" class="sidebar">
      <div class="logo">小说阅读管理</div>
      <el-menu
        :default-active="activeMenu"
        class="sidebar-menu"
        background-color="#545c64"
        text-color="#fff"
        active-text-color="#ffd04b"
        router
      >
        <el-menu-item index="/admin/users">
          <el-icon><User /></el-icon>
          <span>用户管理</span>
        </el-menu-item>
        <el-menu-item index="/admin/books">
          <el-icon><Reading /></el-icon>
          <span>书籍管理</span>
        </el-menu-item>
        <el-menu-item index="/admin/news">
          <el-icon><Message /></el-icon>
          <span>新闻管理</span>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <!-- 主体内容 -->
    <el-container>
      <!-- 头部 -->
      <el-header class="header">
        <div class="header-left">
          <h1>后台管理</h1>
        </div>

        <div class="header-right">
            <el-button style="margin-right:100px " @click="router.push('/')">回到首页</el-button>
          <el-dropdown @command="handleCommand">
            <span class="admin-info">
              <el-avatar :size="30" icon="UserFilled" />
              <span class="admin-name">{{ adminName }}</span>
              <el-icon><ArrowDown /></el-icon>
            </span>

            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="home">回到首页</el-dropdown-item>
                <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <!-- 主要内容区域 -->
      <el-main>
        <div class="admin-container">
          <router-view />
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'
import { User, Reading, Message, UserFilled, ArrowDown } from '@element-plus/icons-vue'
import { removeAuthInfo } from '@/utils/authStorage'

// 获取路由实例
const router = useRouter()
const route = useRoute()

// 当前激活菜单
const activeMenu = ref('/admin/users')

// 管理员名称（可以从localStorage或store中获取）
const adminName = ref('管理员')

// 处理下拉菜单命令
const handleCommand = (command) => {
  if (command === 'logout') {
    ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      // 清除认证信息
      removeAuthInfo()
      // 跳转到登录页
      router.push('/login')
      ElMessage.success('已退出登录')
    }).catch(() => {
      // 用户取消操作
    })
  } else if (command === 'home') {
    // 跳转到首页
    router.push('/')
  }
}

// 监听路由变化
watch(
  () => route.path,
  (newPath) => {
    // 根据当前路由路径设置激活菜单
    if (newPath.startsWith('/admin/users')) {
      activeMenu.value = '/admin/users'
    } else if (newPath.startsWith('/admin/books')) {
      activeMenu.value = '/admin/books'
    } else if (newPath.startsWith('/admin/news')) {
      activeMenu.value = '/admin/news'
    }
  },
  { immediate: true }
)

// 组件挂载时初始化
onMounted(() => {
  // 根据当前路由路径设置激活菜单
  if (route.path.startsWith('/admin/users')) {
    activeMenu.value = '/admin/users'
  } else if (route.path.startsWith('/admin/books')) {
    activeMenu.value = '/admin/books'
  } else if (route.path.startsWith('/admin/news')) {
    activeMenu.value = '/admin/news'
  }
})
</script>

<style scoped>
.admin-layout {
  display: flex;
  height: 100vh;
}

.sidebar {
  background-color: #545c64;
  color: white;
  overflow: hidden;
}

.logo {
  height: 60px;
  line-height: 60px;
  text-align: center;
  font-size: 18px;
  font-weight: bold;
  color: #fff;
  background-color: #42b983;
}

.sidebar-menu {
  border: none;
  height: calc(100% - 60px);
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
  border-bottom: 1px solid #dcdfe6;
  padding: 0 20px;
}

.header-left h2 {
  margin: 0;
  color: #303133;
}

.header-right {
  display: flex;
  align-items: center;
}

.admin-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.admin-name {
  margin: 0 10px;
  color: #606266;
}

.admin-container {
  padding: 0;
}

.el-main {
  background-color: #f5f5f5;
  padding: 20px;
}
</style>